package Server.Date;

import java.io.*;
import java.util.Scanner;

import java.util.*;

import org.apache.mina.core.session.IoSession;

import Server.Timp.Sarcina;


public class ReadWriteTextFile{
	public static Vector<String> vDate = new Vector<String>();
	
	public static long [] lTime;
	
	public static int lTimeLength;

  /**
  * Fetch the entire contents of a text file, and return it in a String.
  * This style of implementation does not throw Exceptions to the caller.
  *
  * @param aFile is a file which already exists and can be read.
  */
  static public void getContents(File aFile) {

	 //...checks on aFile are elided
  //  StringBuilder contents = new StringBuilder();
    
    long lMilisecS = 0;
    long lSecS = 0;
    long lMinutS = 0;
    long lTimpS = 0;
    
    long lMilisecE = 0;
    long lSecE = 0;
    long lMinutE = 0;
    long lTimpE = 0;
        
    try {
    	
      //use buffering, reading one line at a time
      //FileReader always assumes default encoding is OK!
      BufferedReader input =  new BufferedReader(new FileReader(aFile));
      System.out.println("Se incarca fisierul cu date...");
      
 
      try {
        String line = null; //not declared within while loop
        String dtTime = null;
        //String dtTime2 = null;
        String broker_date = null;
        
        int k = 0;
                
        // verific numarul de linii din fisierul citit
        while (( line = input.readLine()) != null)
        	k++;
        
        System.out.println("Fisierul citit are " + k + " linii...");
        
        // se muta la prima pozitie ???
        input.close();
        input = new BufferedReader(new FileReader(aFile));
        
        lTime = new long[k]; // contine diferentele in milisecunde dintre 2 linii
        
        k = -1;
        
        System.out.println("Se citeste linie cu linie fisierul si se extrag minutul, secunda si milisecunda...");
                
        while (( line = input.readLine()) != null){
        	// parsez linia citita
        	Scanner scan = new Scanner(line);
        	
        	// extrag data (data + timpul) si adaug in vDate valorile bursiere
        	scan.useDelimiter(",");
        	dtTime = scan.next(); //retine data
        	broker_date = scan.next()+", " + scan.next() + ", " + scan.next();
        	vDate.add(broker_date);
        	
        	// extrag timpul (minutul, secunda, milisecunda)
        	Scanner scan1 = new Scanner(dtTime);
        	scan1.useDelimiter(" ");
        	scan1.useDelimiter(":");
        	scan1.next();
        	lMinutS = Long.parseLong(scan1.next());
        	dtTime = scan1.next();
        	        	
        	Scanner scan2 = new Scanner(dtTime);
        	scan2.useDelimiter("\\.");
        	lSecS = Long.parseLong(scan2.next());
        	lMilisecS = Long.parseLong(scan2.next());
        	
        	// calculez timpul in milisecunde
        	// nu tin cont de ora, doar de minut, secunda si milisecunda
        	lTimpS = lMilisecS + lSecS*1000 + lMinutS*60*1000; 
        	   
        	// verific daca sunt la prima iteratie
        	if ((lMinutE == 0) && (lSecE == 0) && (lMilisecE == 0)){
        		lMinutE = lMinutS;
        		lSecE = lSecS;
        		lMilisecE = lMilisecS;
        		lTimpE = lMilisecE + lSecE*1000 + lMinutE*60*1000;
        	} else {
        		// sunt in iteratia n (n = 2..NrMaximLinii)
        		k++; // contorul sirului long
        		lTime[k] = lTimpS - lTimpE; //calculez diferenta de milisecunde intre 2 linii citite
        		
        		lMinutE = lMinutS;
        		lSecE = lSecS;
        		lMilisecE = lMilisecS;
        		lTimpE = lMilisecE + lSecE*1000 + lMinutE*60*1000;
        		
        		//System.out.println(dtTime + " Min: " + lMinutS + ", sec: " + lSecS + ", milisec: " +
        		//		lMilisecS + ", Diferenta: " + lTime[k] );        		
        	}       	
        }
        lTimeLength = lTime.length;
        System.out.println("gata incarcarea fisierului in memorie... nr. de elemnte: " + lTimeLength);
        System.out.println("intrare intr-un ciclu infinit...");       
      }
      finally {
        input.close();
        
      }
    }
    catch (IOException ex){
      ex.printStackTrace();
    }
    
  }

   
 
  public static void initializare () throws IOException {
   File testFile = new File("00_ticks.txt");
     
   //parsare fisier
   getContents(testFile);
    
  }
  
  public void start(){
	  int j = 0;
	   long k = 0;
	  
	   
	   try{
		   initializare();
	   } catch (IOException e){
		   e.printStackTrace();
	   }
	   int iNrLinii = lTimeLength;
	   System.out.println("am terminat initializarea... ");
	   
	   Sarcina src = new Sarcina(System.currentTimeMillis(),vDate);// sarcina executata
	   
	   System.out.println("am terminat initializarea clasei Sarcina... ");
	   //System.out.println("Sesiunea " + session.getId() );
	   
	   
	 /**  while (j != 1)
		 synchronized(src){	   
			System.out.println("inainte de for, session este " + session.isReadSuspended()); 
	      for(int i = 0; i < iNrLinii; i++){			
	    	  //System.out.println("in for... ");
	    	  		k = lTime[i];
		    	   //System.out.println(i + " valoarea citita: " + k + " = ");// doar de test
	    	  		
	    	  		session.write(k + "\n");
		    	   try {
						src.wait(k); //asteapta k milisecunde
						src.Afisare(session);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
	    	   }       
	       }//sf sync   */
  }
} 



